module namespace resultview = "http://www.xmlproject.com/smi/lib/view/resultview";

(:~
 : Renders two searchresult xml fragments into one html table with two columns. 
 :
 : @param $searchresult1 the first searchresult will be shown in the left column  
 : @param $searchresult2 the second searchresult will be shown in the right column  
 : @return returns an HTML table containing the two rendered searchresults
 :)
declare function resultview:show($searchresult1 as element(searchresult),$searchresult2 as element(searchresult)) as element()
{
    (: google gives us only 8 results, so we restrict twitter to 8 as well :)
    
    let $numberofresults := 8
    return
        <table>
            <tr style="vertical-align: top;">
                <td style="width:50%">
                    {
                        resultview:renderSearchResult($searchresult1,$numberofresults)
                    }
                </td>
                <td style="width:50%">
                    {
                        resultview:renderSearchResult($searchresult2,$numberofresults)
                    }
                </td>
            </tr>
        </table>
};


(:~
 : Renders a searchresult xml fragment to an html table.
 :
 : @param $searchresult the searchresult representation containing the provider and results  
 : @param $numberofresults the number of results that should be rendered into the html table   
 : @return returns an HTML table
 :)
declare function resultview:renderSearchResult($searchresult as element(searchresult), $numberofresults as xs:integer) as element()
{
    let $provider := $searchresult/provider/text()
    return
        <table style="height: 100%;">
            <tr>
                <td><h2>{$provider}</h2></td>
            </tr>
            {
                for $result in $searchresult/result[position() < $numberofresults]
                let $title := data($result/@title)
                let $href := data($result/@href)
                
                (: get rid of all line feeds and carriage returns :)
                let $content := normalize-space($result/text()) 
                return 
                    <tr>
                        <td>
                            <h3><a href="{$href}">{$title}</a></h3>
                            <p>{$content}</p>
                        </td>
                    </tr>
            } 
        </table>
};
